package com.project.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.auth.model.system.SysUser;
import com.project.auth.service.SysUserService;
import com.project.auth.vo.system.SysUserQueryVo;
import com.project.common.md5.MD5;
import com.project.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description
 * @Author : XIAOMING
 * @create 2023/4/6 10:07
 */
@Api(tags = "用户管理接口")
@RestController
@CrossOrigin
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    //用户条件查询
    @ApiOperation("用户条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page, @PathVariable Long limit, SysUserQueryVo sysUserQueryVo) {
        //创建page对象
        Page<SysUser> pageParm = new Page<>(page, limit);
        //封装条件，判断条件值不为空
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        //获取条件值
        String username = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        //判断条件值不为空
        if (StringUtils.isNotEmpty(username)) {
            wrapper.like(SysUser::getUsername, username);
        }
        if (StringUtils.isNotEmpty(createTimeBegin) && StringUtils.isNotEmpty(createTimeEnd)) {
            //大于开始时间，小于结束时间
            wrapper.ge(SysUser::getCreateTime, createTimeBegin).le(SysUser::getCreateTime, createTimeEnd);
        }
        //调用方法实现分页查询
        IPage<SysUser> pageModel = sysUserService.page(pageParm, wrapper);
        return Result.ok(pageModel);
    }

    @ApiOperation("获取用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysUser user = sysUserService.getById(id);
        return Result.ok(user);
    }

    @ApiOperation("保存用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser user) {
        //使用md5加密密码
        String password = user.getPassword();
        String pwdMd5 = MD5.encrypt(password);
        user.setPassword(pwdMd5);

        sysUserService.save(user);
        return Result.ok();
    }

    @ApiOperation("更新用户")
    @PutMapping("update")
    public Result updateById(@RequestBody SysUser user) {
        sysUserService.updateById(user);
        return Result.ok();
    }

    @ApiOperation("删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        sysUserService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysUserService.updateStatus(id, status);
        return Result.ok();
    }

    /**
     * 获取当前用户
     * @return
     */
    @GetMapping("getCurrentUser")
    public Result getCurrentUser() {
        Map<String, Object> map = sysUserService.getCurrentUser();

        return Result.ok(map);
    }
}
